#include "../apue.h"

static void pr_ids(char * name)
{
    long tpgid = (long)tcgetpgrp(STDIN_FILENO);

    printf("%s pid = %ld, ppid = %ld, pgid = %ld, tpgid = %ld\n",
            name, (long)getpid(), (long)getppid(), (long)getpgid(0),
            tpgid);

    if (tpgid < 0)
        printf("no controlling terminal\n");

}

int main(void)
{
    pid_t pid;
    if ((pid = fork()) < 0)
        err_sys("fork error");
    else if (pid == 0)
    {
        printf("before setsid:\n");
        pr_ids("child");
        setsid();
        printf("after setsid:\n");
        pr_ids("child");
    } else {
        pr_ids("parent");
    }

    exit(0);
}
